package com.zone.vehicle.superApps.view.setting;

import android.os.Bundle;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;

import com.zone.vehicle.superApps.databinding.ActivitySettingBinding;
import com.zone.vehicle.superApps.view.common.base.BaseActivity;
import com.zone.vehicle.superApps.view.common.listener.ActionEnum;
import com.zone.vehicle.superApps.view.common.listener.ActionListener;

public class SettingActivity extends BaseActivity {

    private ActivitySettingBinding binding;

    private FragmentPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initPagerAdapter();
        binding.viewpager.setAdapter(adapter);
        binding.viewpager.setCurrentItem(0);
    }

    @Override
    protected void initFragmentPages() {
        addPage(new SettingFragment(listener));
        addPage(new SuggestionFragment());
    }

    @Override
    protected void setContentView() {
        binding = ActivitySettingBinding.inflate(LayoutInflater.from(this));
        setContentView(binding.getRoot());
    }

    private void initPagerAdapter(){
        adapter = new FragmentPagerAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            @Override
            public int getCount() {
                return getPages().size();
            }

            @NonNull
            @Override
            public Fragment getItem(int position) {
                return getPage(position);
            }
        };
    }

    ActionListener listener = new ActionListener() {
        @Override
        public boolean onAction(ActionEnum action) {
            return false;
        }

        @Override
        public boolean onPageChanged(String pageName) {
            if (pageName.equals(SettingFragment.class.getSimpleName())){
                binding.viewpager.setCurrentItem(0);
            }else if (pageName.equals(SuggestionFragment.class.getSimpleName())){
                binding.viewpager.setCurrentItem(1);
            }
            return false;
        }
    };

}